package thread;

public class Demo9 {

    public static Object locker = new Object();
    public static int currentThread = 1;

    public static void main(String[] args) {
        Thread a = new Thread(() -> {
            synchronized (locker) {
                while (currentThread != 1) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("c");
                currentThread = 2;
                locker.notifyAll();
            }
        });
        Thread b = new Thread(() -> {
            synchronized (locker) {
                while (currentThread != 2) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("b");
                currentThread = 3;
                locker.notifyAll();
            }
        });
        Thread c = new Thread(() -> {
            synchronized (locker) {
                while (currentThread != 3) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("a");
                currentThread = 1;
                locker.notifyAll();
            }
        });

        a.start();
        b.start();
        c.start();
    }
}
